var payCenterEvent = function() {};
payCenterEvent.prototype = {

	//app支付
	appPay: function(channel, orderID, userID, mainorderno) {
		var url = ('alipay' == channel.id) ? CONFIG_MANAGER.aliPayServer : CONFIG_MANAGER.weixinPayServer;
		var payFailUrl = "payFail.html?mainorderno=" + mainorderno;
		var returnUrl = "paySuccess.html?mainorderno=" + mainorderno;		
		console.log(JSON.stringify(channel));
		console.log(url);
		var requestData = {
			orderid: orderID,
			memberid: userID,
			type: 1,
			remark: '',
			showurl: '',
			returnurl: returnUrl,
		};
		console.log("app订单请求信息：" + JSON.stringify(requestData));
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			contentType: 'application/json',
			data: JSON.stringify(requestData),
			beforeSend:function(){ $.showWaiting(); }
		}).done(function(order) {
			if('alipay' == channel.id){
				console.log('app支付接口返回：' +  order);
			}else{
				console.log('app支付接口返回：' +  JSON.stringify(order));
			}			
			plus.payment.request(channel, order, function(result) {
				console.log(channel.description + '支付成功：' + JSON.stringify(result));
				$.alert(channel.description + "支付成功", function() {
						redirect("paySuccess.html?mainorderno=" + mainorderno);
					},
					"支付",
					"确定"
				);
			}, function(e) {
				console.log(channel.description + "支付失败：[" + e.code + "]：" + e.message);
				redirect("payFail.html?type=" + channel.id + "&mainorderno=" + mainorderno);
			});
		}).fail(function(data){
			$.toast(data.responseJSON.Message);
		}).always(function(){
			$.closeWaiting();
		});
	},
	//wap移动端支付
	webPay: function(type, orderID, userID, mainorderno) {
		var url = ('alipay' == type) ? CONFIG_MANAGER.aliPayWebServer : CONFIG_MANAGER.weixinPayWebServer;
		var payFailUrl = "http://" + window.location.host + "/soolife_app/view/shopcart/payFail.html?mainorderno=" + mainorderno;
		var returnUrl = "http://" + window.location.host + "/soolife_app/view/shopcart/paySuccess.html?mainorderno=" + mainorderno;
		var requestData = {
			orderid		: orderID,
			memberid	: userID,
			type		: 1,
			remark		: '',
			showurl		: payFailUrl,
			returnurl	: returnUrl
		};
		console.log("订单信息" + JSON.stringify(requestData));
		$.ajax({
			type: "POST",
			url: url,
			async: true,
			contentType: 'application/json',
			data: JSON.stringify(requestData),
			beforeSend: function() {
				$.showWaiting();
			}
		}).done(function(order) {
			console.log('支付接口返回：' + order);
			window.location.href = order;
		}).fail(function(data) {
			$.closeWaiting();
			$.toast(data.responseJSON.Message);
		}).always(function() {
			$.closeWaiting();
		});
	},
	//钱包余额支付
	welletPay: function(orderID, user, passWord, mainorderno) {
		var postData = {
			orderid	: orderID, 
			type	: 1, 
			memberid: user.id, 
			password: passWord,
			remark	: '',
		};
		$.ajax({
			type:"POST",
			url: CONFIG_MANAGER.walletPayServer,
			async:true,
			headers:{ token:user.token },
			contentType: 'application/json',
			data: JSON.stringify(postData),
			beforeSend:function(){ $.showWaiting(); }
		}).done(function(data){
			//更新用户信息
      $.ajax({
          headers: { "token": user.token },
          type: "GET",
          url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id
      }).done(function (data) {
          setLocalStorage(STORAGE_MANAGER.currentmember, data);
          if (AGENT_CLIENT.APP) {
          	plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
          }
          redirect("paySuccess.html?mainorderno=" + mainorderno);	
      }).fail(function(data){
      	$.toast(data.responseJSON.Message);
      });
		}).fail(function(data){
			$.toast(data.responseJSON.Message);
			setTimeout(function(){ redirect("payFail.html?mainorderno=" + mainorderno); }, 1000);
		}).always(function(){
			$.closeWaiting();
		});
	},
	//钱包支付和第三方支付切换
	changePayType: function() {
		$("#main").on("click", "#checkbox-1-1_wallet", function() {
			if ($(this).is(':checked')) {
				var isSetPassword = getLocalStorage(STORAGE_MANAGER.issetpaypasswd);
				var currentLoginUser = getLocalStorage(STORAGE_MANAGER.currentmember);
				if(!isSetPassword){
					$.alert("请先设置支付密码", function(e) {
						redirectUrl = "../account/validatePhone.html?type=pay&phone="+ currentLoginUser.phone +"&backurl=shopcart|payCenter.html" + window.location.search;
						redirect(redirectUrl);
					});
					return;
				}
				var pay_fee = parseFloat($("#pay_fee").html());
				var money = parseFloat($("#money").html());
				var deliveryFee = parseFloat($("#delivery_fee").html());
				if (money >= (pay_fee+deliveryFee)) {
					$("#payPassword").show();
					$("#payPassword #password").val("");
					$("#wxpay").attr("checked", false);
					$("#alipay").attr("checked", false);
				} else {
					$.toast("钱包余额不足");
					$(this).attr("checked", false);
				}
			} else {
				$("#payPassword").hide();
			}
		});
		$("#main").on("click", "#wxpay,#alipay", function() {
			$("#checkbox-1-1_wallet").attr("checked", false);
			$("#payPassword").hide();
		});
	},
	//隐藏订单详情
	showOrderDetail: function() {
		$("#main").on("click", "#showOrderDetail", function() {
			if ($(this).siblings().is(":hidden")) {
				$(this).siblings().show();
			} else {
				$(this).siblings().hide();
			}

		});
	}



}